﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Management;
using System.Windows.Forms;
using Dolinay;
using System.IO;

namespace TestImport.View.usb.SimpleDetector
{
    public partial class USBcheckForm : Form
    {
        private DriveDetector driveDetector = null;

        public USBcheckForm()
        {
            InitializeComponent();
            loadDevices();
            driveDetector = new DriveDetector();
            driveDetector.DeviceArrived += new DriveDetectorEventHandler(OnDriveArrived);
            driveDetector.DeviceRemoved += new DriveDetectorEventHandler(OnDriveRemoved);
            driveDetector.QueryRemove += new DriveDetectorEventHandler(OnQueryRemove);
        }

        // Called by DriveDetector when removable device in inserted 
        private void OnDriveArrived(object sender, DriveDetectorEventArgs e)
        {
            // Report the event in the listbox.
            // e.Drive is the drive letter for the device which just arrived, e.g. "E:\\"
            string s = e.Drive;
            lbDisks.Items.Add(s);

            // If you want to be notified when drive is being removed (and be able to cancel it), 
            // set HookQueryRemove to true 
        }

        // Called by DriveDetector after removable device has been unpluged 
        private void OnDriveRemoved(object sender, DriveDetectorEventArgs e)
        {
            // TODO: do clean up here, etc. Letter of the removed drive is in e.Drive;

            // Just add report to the listbox
            string s = e.Drive;
            lbDisks.Items.Remove(s);
        }

        // Called by DriveDetector when removable drive is about to be removed
        private void OnQueryRemove(object sender, DriveDetectorEventArgs e)
        {
            //voor als ge da zou doen
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        public void loadDevices()
        {
            // browse all USB WMI physical disks

            foreach (ManagementObject drive in new ManagementObjectSearcher("select * from Win32_DiskDrive where InterfaceType='USB'").Get())
            {
                foreach (ManagementObject partition in new ManagementObjectSearcher("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + drive["DeviceID"]
                + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition").Get())
                {
                    // associate partitions with logical disks (drive letter volumes)
                    foreach (ManagementObject disk in new ManagementObjectSearcher("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + partition["DeviceID"]
                    + "'} WHERE AssocClass =Win32_LogicalDiskToPartition").Get())
                    {
                        lbDisks.Items.Add(disk["Name"]+"\\");
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (lbDisks.SelectedItem != null)
            {
                string disk = lbDisks.SelectedItem.ToString();
                USBkiesDataForm f = new USBkiesDataForm(disk);
                f.ShowDialog();
                this.Close();
            }
            //sluit hierna toch nog
        }
    }
}